package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: shikong
 * @Date: 2020/7/26 10:57
 */

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/payment/insert")
    public CommonResult insertPayment(@RequestBody  Payment payment){

        int result = paymentService.insertPayment(payment);

        if (result>0){
            return new CommonResult(200,"successufl,port:"+port,result);
        }else {
            return new CommonResult(444,"error",result);
        }
    }

    //单机 http://localhost:8001/payment/get/1
    //eureka http://localhost:8001/payment/get/1
    @GetMapping("/payment/get/{id}")
    public CommonResult insertPayment(@PathVariable long id){

        Payment payment= paymentService.getPaymentById(id);
        if (null != payment){
            return new CommonResult(200,"successufl,port:"+port,payment);
        }else {
            return new CommonResult(444,"error",null);
        }
    }

    @GetMapping("/payment/getPort")
    public String insertPayment(){

        return port;
    }

    @GetMapping("/payment/discovery")
    public Object discoveryClient(){

        log.info("接收到请求");
        return discoveryClient.getServices();

    }

    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout(){

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return port;

    }




}
